/*globals TJ, jQuery */
(function ($) {

    function RandomNumberGenerator() {
        TJ.Module.call(this);
        var self = this,
            getRandomNumber = null,
            updateRandomNumber = null;

        // Module specific logic goes here
        this.main = function ($ctx, callback) {
            updateRandomNumber();
            callback();
        };

        getRandomNumber = function () {
            return parseInt(Math.random() * 10000, 10);    
        };

        updateRandomNumber = function (timer) {
            clearTimeout(timer);
            timer = setTimeout(function (timer) {
                var rn = getRandomNumber();
                $('.rn', self.$bd).html(rn);
                updateRandomNumber(timer);

                self.get_connectors_context().trigger('randomUpdate',[rn]);
            }, 2000);
        };
    }

    TJ.integrate_module(RandomNumberGenerator, 'RandomNumberGenerator');
}(jQuery));